package com.gba.mark.exam.controller;


import com.gba.mark.exam.entity.ExamAnswer;
import com.gba.mark.exam.entity.dto.ExamAnswerDto;
import com.gba.mark.exam.service.ExamAnswerService;
import com.gba.mark.group.service.IGroupService;
import com.gba.mark.user.service.user.IUserService;
import com.gba.mark.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/exam/answer")
public class ExamAnswerController {

    @Autowired
    private ExamAnswerService examAnswerService;

    @Resource
    private IUserService userService;

    @Resource
    private IGroupService groupService;

    /**
     * 新建并保存一份用户输入的答案
     */
    @PostMapping("/saveAnswer")
    public ResponseUtil<Boolean> saveAnswer(Authentication authentication,
                                        @RequestBody List<ExamAnswerDto> examAnswerDto,
                                        @RequestParam String groupName){
        Integer userId = userService.getUserIdByAuth(authentication);
        Integer groupId = groupService.getGroupIdByAuthAndName(authentication, groupName);
        return new ResponseUtil<>(HttpStatus.OK, "",
                examAnswerService.saveAnswer(examAnswerDto, userId, groupId));
    }
}


